package com.jwdf.uda.ui.activity.presenter;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.UploadImage;
import com.jwdf.uda.bean.UploadImageResponse;
import com.jwdf.uda.http.HttpHelper;
import com.jwdf.uda.http.action.UploadImageAction;
import com.jwdf.uda.ui.activity.view.IAView;
import com.jwdf.uda.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 类说明：
 * 作者：huangqiuxin on 2016/6/8 15:06
 * 邮箱：648859026@qq.com
 */
public abstract class AUploadImagePresenter<T extends IAView> extends APresenter<T> {
    protected abstract List<UploadImage> getUploadImageList();
    protected abstract void doUploadImage(UploadImage uploadImage);
    protected abstract void doUploadImagesSuccess(List<String> imageTokens);

    public AUploadImagePresenter(T iView) {
        super(iView);
    }

    protected void doUploadImagesAction() {
        List<UploadImage> uploadImages = getUploadImageList();
        if (uploadImages == null) return;
        boolean hasImageToken = true;
        for (int i=0;i<uploadImages.size();i++){
            UploadImage image = uploadImages.get(i);
            if (StringUtils.isNullEmpty(image.getImageToken())) {
                hasImageToken = false;
                doUploadImageAction(StringUtils.filterNullAndTrim(image.getImageUrl()));
            }
        }
        if(hasImageToken){
            doHasImageTokenSuccess();
        }
    }

    private void doUploadImageAction(final String imageUrl) {

        UploadImageAction action = new UploadImageAction(getContext(), imageUrl);
        mIView.getFragment().onNewRequestCall(action.request(new UploadImageAction.IResCallback<String>() {
            @Override
            public void onCallback(int resultCode, UploadImageResponse response) {
                if (mIView.getFragment().getView() != null) {
                    if (HttpHelper.showError(getContext(), resultCode, response, getContext().getString(R.string.error_operation))) {
                        mIView.getFragment().cancelRequest();
                        mIView.getFragment().closeProgressDialog();
                        return;
                    }
                    doUploadImageResponse(imageUrl, response);
                }
            }
        }));


//        final  File uploadImageFile = null;
//        File imageFile = null;
//        try {
//            imageFile = new File(new URI(imageUrl));
//        } catch (URISyntaxException e) {
//            e.printStackTrace();
//        }
//        final String imageType = ImageProcessing.getImageType(imageFile.getAbsolutePath());
//        final String filePath = imageFile.getAbsolutePath();
////        try {
//////            uploadImageFile = ImageProcessing.createTempImageFile(imageType);//将要保存图片的路径
////        } catch (IOException e) {
////            e.printStackTrace();
////        }
//        Luban.get(AppContext.getInstance())
//                .load(imageFile)
//                .putGear(Luban.THIRD_GEAR)
//                .setCompressListener(new OnCompressListener() {
//                    @Override
//                    public void onStart() {
//
//                    }
//
//                    @Override
//                    public void onSuccess(File file) {
//                        Log.i("----------",imageUrl+" compress success : ");
//                            UploadImageAction action = new UploadImageAction(getContext(), file);
//                            mIView.getFragment().onNewRequestCall(action.request(new UploadImageAction.IResCallback<String>() {
//                                @Override
//                                public void onCallback(int resultCode, UploadImageResponse response) {
//                                    if (mIView.getFragment().getView() != null) {
//                                        if (HttpHelper.showError(getContext(), resultCode, response, getContext().getString(R.string.error_operation))) {
//                                            mIView.getFragment().cancelRequest();
//                                            mIView.getFragment().closeProgressDialog();
//                                            return;
//                                        }
//                                        doUploadImageResponse(imageUrl, response);
//                                    }
//                                }
//                            }));
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//
//                    }
//                }).launch();


    }

    private void doUploadImageResponse(String imageUrl, UploadImageResponse response) {
        doUploadImage(new UploadImage(imageUrl, response.getToken()));

        doHasImageTokenSuccess();
    }

    //判断是否图片已经上传完成
    private void doHasImageTokenSuccess(){
        List<String> imageTokens = getImageTokenList();
        if (imageTokens != null && imageTokens.size() > 0) {
            doUploadImagesSuccess(imageTokens);
        }
    }

    protected List<String> getImageTokenList() {
        List<String> imageTokens = new ArrayList<>();
        List<UploadImage> uploadImages = getUploadImageList();
        if (uploadImages == null) return imageTokens;
        for (int i=0;i<uploadImages.size();i++) {
            UploadImage image = uploadImages.get(i);
            if (!StringUtils.isNullEmpty(image.getImageToken())) {
                imageTokens.add(image.getImageToken());
            }else{
                return null;
            }
        }
        return imageTokens;
    }

}
